package com.xmut.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xmut.mall.product.annotation.CheckJwt;
import com.xmut.mall.product.entity.BrandEntity;
import com.xmut.mall.product.service.BrandService;
import com.xmut.mall.product.utils.RespBean;
import com.xmut.mall.product.vo.AddCateVo;
import com.xmut.mall.product.vo.Catelog2Vo;
import com.xmut.mall.product.vo.EditCateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.product.entity.CategoryEntity;
import com.xmut.mall.product.service.CategoryService;
import com.xmut.mall.product.utils.PageUtils;
import com.xmut.mall.product.utils.R;



/**
 * 商品三级分类
 *
 * @author 1912120131suntingxing
 * @email 1192081604@qq.com
 * @date 2022-07-14 00:45:33
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取适配前端的Cascader组件的分类树
     * @return
     */
    @CheckJwt
    @GetMapping("/getCateByCascader")
    public RespBean getCateByCascader(){
        return categoryService.getCateByCascader();
    }

    /**
     * 获取分类选择组件的数据结果
     * @return
     */
    @GetMapping("/getCateTreeComponent")
    public RespBean getCateTreeComponent(){
        return categoryService.getCateTreeComponent();
    }

    /**
     * 根据Id获取子分类(只有一级)
     * @param cateId
     * @return
     */
    @CheckJwt
    @GetMapping("/getCateTreeById/{cateId}")
    public RespBean getCateTreeById(@PathVariable Long cateId){
        return categoryService.getCateTreeById(cateId);
    }

    /**
     * 启用分类
     * @param cateId
     * @return
     */
    @CheckJwt
    @PutMapping("/enableCate/{cateId}")
    public RespBean enableCate(@PathVariable Long cateId){
        return categoryService.enableCate(cateId);
    }

    /**
     * 禁用该分类
     * @param cateId
     * @return
     */
    @CheckJwt
    @PutMapping("/disableCate/{cateId}")
    public RespBean disableCate(@PathVariable Long cateId){
        return categoryService.disableCate(cateId);
    }

    /**
     * 编辑分类信息
     * @param vo
     * @return
     */
    @CheckJwt
    @PutMapping("/editCate")
    public RespBean editCate(@RequestBody EditCateVo vo){
        return categoryService.editCate(vo);
    }

    /**
     * 添加分类
     * @param vo
     * @return
     */
    @CheckJwt
    @PostMapping("/addCate")
    public RespBean addCate(@RequestBody AddCateVo vo){
        return categoryService.addCate(vo);
    }


    /**
     * 根据cateId获取它的父分类将其组合起来
     * @param catalogId
     * @return
     */
    @GetMapping("/getCateParentAndMerge/{catalogId}")
    public RespBean getCateParentAndMerge(@PathVariable Long catalogId){
        List<CategoryEntity> categoryEntities =  categoryService.getCateParentAndMerge(catalogId);
        return RespBean.success("获取分类组成功",categoryEntities);
    }

    /**
     * 获取商品的一级分类
     * @return
     */
    @GetMapping("/getIndexCateLevel1")
    public RespBean getIndexCateLevel1(){
        List<CategoryEntity> level1 = categoryService.getCateLogLevel1();
        return RespBean.success("获取一级分类成功",level1);
    }

    /**
     * 获取商品的二级分类和三级分类
     * @return
     */
    @GetMapping("/getIndexCateLevel2andLevel3")
    public RespBean getIndexCatelog(){
        Map<String, List<Catelog2Vo>> catelog = categoryService.getIndexCateLevel2andLevel3();
        return RespBean.success("获取分类成功",catelog);
    }

    /**
     * 获取三级分类树
     * @return
     */
    @GetMapping("/getCateTree")
    public RespBean getCateTree(){
        List<CategoryEntity> list = categoryService.getCateTree();
        return RespBean.success("获取成功",list);
    }

    /**
     * 查出所有的分类以及子分类，以树形结构组装起来返回给前端
     */
    @RequestMapping("/list/tree")
    public R list(){

        List<CategoryEntity> entities = categoryService.listWithTree();

        return R.ok().put("data", entities);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    @RequestMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
        categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
        // 检查将被删除的菜单是否被别的地方引用
        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
